<?php

namespace app\user\controller;

use app\model\UserAddress as ModelUserAddress;
use app\user\logic\User as userLogicUser;

/**
 * 地址相关类
 * Class Common
 * @package app\user\controller
 */
class Address extends Member
{
    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 收（寄）件人地址添加 \n
     * URI : /address.add
     * @param :
     *  name      |  type  | null | description
     * -----------|--------|------|-------------
     *  passport  | string | 必填  | 登录凭证
     *  type      | string | 必填  | 类型（send:寄件，receive:收件）
     *  name      | string | 必填  | 姓名
     *  phone     | string | 必填  | 联系方式
     *  provId    | int    | 必填  | 省份id
     *  cityId    | int    | 必填  | 城市id
     *  countyId  | int    | 必填  | 县(区)id
     *  streetId  | int    | 选填  | 街道id
     *  address   | string | 必填  | 详细地址
     *  lng       | string | 必填  | 经度
     *  lat       | string | 必填  | 纬度
     *
     * @return
     *  name        |  type  | description
     * -------------|--------|-------------
     *  ----------- | ------ | 无
     *
     */
    public function addUserAddress()
    {
        $data['type'] = $this->_getContentParams('type');
        _toCompare(in_array($data['type'], ['receive', 'send']), '==', false, 10014);

        $data['name'] = $this->_getContentParams('name');
        _toCompare(empty($data['name']), '==', true, 12003);

        $data['phone'] = $this->_getContentParams('phone');
        _toValidate('mobile', $data['phone'], 10013);

        //省份id;
        $data['prov_id'] = $this->_getContentParams('provId');
        $data['prov_name'] = userLogicUser::getInstance()->checkAreaId($data['prov_id']); //验证省份id
        _toCompare(empty($data['prov_name']), '==', true, 10009);

        //城市id;
        $data['city_id'] = $this->_getContentParams('cityId');
        $data['city_name'] = userLogicUser::getInstance()->checkAreaId($data['city_id'], $data['prov_id']); //验证城市id
        _toCompare(empty($data['city_name']), '==', true, 10009);

        //县(区)id;
        $data['county_id'] = $this->_getContentParams('countyId');
        $data['county_name'] = userLogicUser::getInstance()->checkAreaId($data['county_id'], $data['city_id']); //验证县区id
        _toCompare(empty($data['county_name']), '==', true, 10009);

        //街道id;
        $data['street_id'] = $this->_getContentParams('streetId');
        if ($data['street_id'] > 0) {
            $data['street_name'] = userLogicUser::getInstance()->checkAreaId($data['street_id'], $data['county_id']); //验证街道
            _toCompare(empty($data['street_name']), '==', true, 10009);
        }

        $data['detail_address'] = $this->_getContentParams('address'); //详细地址;
        _toCompare(empty($data['detail_address']), '==', true, 12004);
        //经度
        $data['lng'] = $this->_getContentParams('lng');
        _toCompare(empty($data['lng']), '==', true, 14015);

        //纬度
        $data['lat'] = $this->_getContentParams('lat');
        _toCompare(empty($data['lat']), '==', true, 14015);

        $data['user_id'] = $this->user_id;

        //添加数据
        $int_address_id = ModelUserAddress::getInstance()->toAdd($data);

        if ($int_address_id > 0) _returnData();
        _returnError(10010);
    }


    /**
     * 收（寄）件人地址修改 \n
     * URI : /address.update
     * @param :
     *  name          |  type  | null | description
     * ---------------|--------|------|-------------
     *  passport      | string | 必填  | 登录凭证
     *  userAddressId | int    | 必填  | 用户地址id
     *  name          | string | 必填  | 姓名
     *  phone         | string | 必填  | 联系方式
     *  provId        | int    | 必填  | 省份id
     *  cityId        | int    | 必填  | 城市id
     *  countyId      | int    | 必填  | 县(区)id
     *  streetId      | int    | 选填  | 街道id
     *  address       | string | 必填  | 详细地址
     *
     * @return
     *  name        |  type  | description
     * -------------|--------|-------------
     *  ----------- | ------ | 无
     */
    public function updateUserAddress()
    {
        $int_address_id = $this->_getContentParams('userAddressId');//用户地址
        $check_res = ModelUserAddress::getInstance()->checkAddressId($int_address_id);
        _toCompare($check_res, '===', false, 12005);

        $data['name'] = $this->_getContentParams('name');
        _toCompare(empty($data['name']), '==', true, 12003);

        $data['phone'] = $this->_getContentParams('phone');
        _toValidate('mobile', $data['phone'], 10013);

        //省份id;
        $data['prov_id'] = $this->_getContentParams('provId');
        $data['prov_name'] = userLogicUser::getInstance()->checkAreaId($data['prov_id']); //验证省份id
        _toCompare(empty($data['prov_name']), '==', true, 10009);

        //城市id;
        $data['city_id'] = $this->_getContentParams('cityId');
        $data['city_name'] = userLogicUser::getInstance()->checkAreaId($data['city_id'], $data['prov_id']); //验证城市id
        _toCompare(empty($data['city_name']), '==', true, 10009);

        //县(区)id;
        $data['county_id'] = $this->_getContentParams('countyId');
        $data['county_name'] = userLogicUser::getInstance()->checkAreaId($data['county_id'], $data['city_id']); //验证县区id
        _toCompare(empty($data['county_name']), '==', true, 10009);

        //街道id;
        $data['street_id'] = $this->_getContentParams('streetId');
        if ($data['street_id'] > 0) {
            $data['street_name'] = userLogicUser::getInstance()->checkAreaId($data['street_id'], $data['county_id']); //验证街道
            _toCompare(empty($data['street_name']), '==', true, 10009);
        }

        $data['detail_address'] = $this->_getContentParams('address'); //详细地址;
        _toCompare(empty($data['detail_address']), '==', true, 12004);
        //经度
        $data['lng'] = $this->_getContentParams('lng');
        _toCompare(empty($data['lng']), '==', true, 14015);

        //纬度
        $data['lat'] = $this->_getContentParams('lat');
        _toCompare(empty($data['lat']), '==', true, 14015);


        //修改数据
        $res = ModelUserAddress::getInstance()->toUpdate(['id' => $int_address_id, 'user_id' => $this->user_id], $data);

        if ($res) _returnData();
        _returnError(10011);
    }


    /**
     * 收（寄）件人地址删除 \n
     * URI : /address.del
     * @param :
     *  name          |  type  | null | description
     * ---------------|--------|------|-------------
     *  passport      | string | 必填  | 登录凭证
     *  userAddressId | int    | 必填  | 用户地址id
     *
     * @return
     *  name        |  type  | description
     * -------------|--------|-------------
     *  ----------- | ------ | 无
     */
    public function delUserAddress()
    {
        $int_address_id = $this->_getContentParams('userAddressId');//用户地址
        $check_res = ModelUserAddress::getInstance()->checkAddressId($int_address_id);
        _toCompare($check_res, '===', false, 12005);

        //删除
        $res = ModelUserAddress::getInstance()->toDelete(['id' => $int_address_id, 'user_id' => $this->user_id]);

        if ($res) _returnData();
        _returnError(10012);
    }


    /**
     * 收（寄）件人地址列表 \n
     * URI : /address.list
     * @param :
     *  name          |  type  | null | description
     * ---------------|--------|------|-------------
     *  passport      | string | 必填  | 登录凭证
     *  type          | string | 必填  | 类型（send:寄，receive:收）
     *  page          |  int   | 必填  | 页码
     *  listRows      |  int   | 必填  | 条数
     *
     * @return
     *  name        |  type  | description
     * -------------|--------|-------------
     *  totalPages  | string | 总页数
     *  list        | array  | 列表
     *
     * list
     *  name         |  type  | description
     * --------------|--------|-------------
     *  id           | int    | 记录id
     *  name         | string | 姓名
     *  phone        | string | 电话
     *  provId       | int    | 省id
     *  provName     | string | 省名称
     *  cityId       | int    | 城市id
     *  cityName     | string | 城市名称
     *  countyId     | int    | 县区id
     *  countyName   | string | 县区名称
     *  streetId     | int    | 街道id
     *  streetName   | string | 街道名称
     * address | string | 具体街道
     */
    public function userExpressList()
    {
        $where['user_id'] = $this->user_id;

        $where['type'] = $this->_getContentParams('type');
        _toCompare(in_array($where['type'], ['receive', 'send']), '==', false, 10014);

        $int_now_page = $this->_getContentParams('page');//当前页码
        _toValidate('id', $int_now_page, 10005);

        $int_list_rows =  $this->_getContentParams('listRows');//一页的条数
        _toValidate('id', $int_list_rows, 10006);

        $int_total_rows = ModelUserAddress::getInstance()->toCount($where);//总条数
        $int_total_pages = ceil($int_total_rows / $int_list_rows);//总页数
        _toCompare(($int_now_page > $int_total_pages && $int_total_pages > 0), '==', true, 10007);
        $int_first_row = ($int_now_page - 1) * $int_list_rows;
        //总列表
        $arr_list = (array)ModelUserAddress::getInstance()->toSelect(
            $where,
            'id,name,phone,prov_id as provId,prov_name as provName,city_id as cityId,city_name as cityName,county_id as countyId,county_name as countyName,street_id as streetId,street_name as streetName,detail_address as address,lng,lat',
            $int_first_row,
            $int_list_rows
        );

        _returnData(['totalPages' => $int_total_pages, 'list' => $arr_list]);
    }
}
